<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 题目1(选做)：const arr=['name','age','hhh','hhh','age','hhh','hhh','hhh'] 
    // 元素各出现多少次
    // 要求输出一个对象如下：{age: 2,hhh: 5,name: 1}
    // 1.
    const arr = ['name', 'age', 'hhh', 'hhh', 'age', 'hhh', 'hhh', 'hhh']
    const counts = {}
    for (const num of arr) {
      counts[num] = counts[num] ? counts[num] + 1 : 1
    }
    console.log(counts);

    // 2.for循环
    // const arr = ['name', 'age', 'hhh', 'hhh', 'age', 'hhh', 'hhh', 'hhh']
    // let obj = {}
    //  for (let i = 0; i< arr.length; i++) {
      // 遍历arr数组，如果有相同的元素，垂涎一次数量加1，没有相同的元素，数量为1
    //    if (obj[arr[i]]) {
    //     obj[arr[i]]++
    //    }
    //    else {
    //     obj[arr[i]] = 1
    //    }
    //  }
    //  console.log(obj);
  </script>
</body>
</html>